package com.mycontract.suitepro.components.dialog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.FragmentManager
import androidx.viewpager.widget.PagerAdapter
import com.mycontract.suitepro.R
import com.mycontract.suitepro.extend.recordContractSuiteClickContractSuiteEvent


/** 结果页展示view pager 适配器 */
class PermissionsAdapter(context: Context, arrayList: List<PermissionItem>) :
    PagerAdapter() {

    private val mContext: Context
    private val dataList: List<PermissionItem>
    private val mPermissionAskDialog = PermissionAskDialog()

    /** fragmentManager */
    private var mfragmentManager: FragmentManager? = null

    init {
        mContext = context
        dataList = arrayList
    }

    /**
     * 点击回调
     *
     * @return Int 位置
     * @return RevObjAiHomeDemo 值
     * @return Int 类型
     */
    private var mItemClickListener: (Int, PermissionItem, Int) -> Unit =
        { action, obj, position -> }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        //子View显示
        val item = dataList[position]
        var view: View =
            View.inflate(mContext, R.layout.view_contract_suite_permission_phone_state, null)
        var mPermissionActionDeny: TextView =
            view.findViewById(R.id.textPermissionPhoneStateActionDeny)
        var mPermissionActionAgree: TextView =
            view.findViewById(R.id.textPermissionPhoneStateActionAgree)
        var mPermissionScrollView: NestedScrollView =
            view.findViewById(R.id.scrollPermissionPhoneStateContent)
        var llPermissionAgreeContent: LinearLayout =
            view.findViewById(R.id.llPermissionPhoneStateAgreeContent)
        var llPermissionActionContent: LinearLayout =
            view.findViewById(R.id.llPermissionPhoneStateActionContent)
        var llPermissionDenyContent: LinearLayout =
            view.findViewById(R.id.llPermissionPhoneStateDenyContent)
        var textPermissionActionCheckbox: TextView =
            view.findViewById(R.id.textPermissionPhoneStateActionCheckbox)
        var imagePermissionActionCheckbox: ImageView =
            view.findViewById(R.id.imagePermissionPhoneStateActionCheckbox)

        if (item.type == PermissionItem.TYPE_SMS) {
            view = View.inflate(mContext, R.layout.view_contract_suite_permission_sms, null)
            mPermissionActionDeny = view.findViewById(R.id.textPermissionSmsActionDeny)
            mPermissionActionAgree = view.findViewById(R.id.textPermissionSmsActionAgree)
            mPermissionScrollView = view.findViewById(R.id.scrollPermissionSmsContent)
            llPermissionAgreeContent = view.findViewById(R.id.llPermissionSmsAgreeContent)
            llPermissionActionContent = view.findViewById(R.id.llPermissionSmsActionContent)
            llPermissionDenyContent = view.findViewById(R.id.llPermissionSmsDenyContent)
            textPermissionActionCheckbox = view.findViewById(R.id.textPermissionSmsActionCheckbox)
            imagePermissionActionCheckbox = view.findViewById(R.id.imagePermissionSmsActionCheckbox)
        }
        if (item.type == PermissionItem.TYPE_CONTRACTS) {
            view = View.inflate(mContext, R.layout.view_contract_suite_permission_contacts, null)
            mPermissionActionDeny = view.findViewById(R.id.textPermissionContactsActionDeny)
            mPermissionActionAgree = view.findViewById(R.id.textPermissionContactsActionAgree)
            mPermissionScrollView = view.findViewById(R.id.scrollPermissionContactsContent)
            llPermissionAgreeContent = view.findViewById(R.id.llPermissionContactsAgreeContent)
            llPermissionActionContent = view.findViewById(R.id.llPermissionContactsActionContent)
            llPermissionDenyContent = view.findViewById(R.id.llPermissionContactsDenyContent)
            textPermissionActionCheckbox = view.findViewById(R.id.textPermissionContactsActionCheckbox)
            imagePermissionActionCheckbox = view.findViewById(R.id.imagePermissionContactsActionCheckbox)
        }
        if (item.type == PermissionItem.TYPE_CALL_LOG) {
            view = View.inflate(mContext, R.layout.view_contract_suite_permission_call_log, null)
            mPermissionActionDeny = view.findViewById(R.id.textPermissionCallLogActionDeny)
            mPermissionActionAgree = view.findViewById(R.id.textPermissionCallLogActionAgree)
            mPermissionScrollView = view.findViewById(R.id.scrollPermissionCallLogContent)
            llPermissionAgreeContent = view.findViewById(R.id.llPermissionCallLogAgreeContent)
            llPermissionActionContent = view.findViewById(R.id.llPermissionCallLogActionContent)
            llPermissionDenyContent = view.findViewById(R.id.llPermissionCallLogDenyContent)
            textPermissionActionCheckbox = view.findViewById(R.id.textPermissionCallLogActionCheckbox)
            imagePermissionActionCheckbox = view.findViewById(R.id.imagePermissionCallLogActionCheckbox)
        }
        if (item.type == PermissionItem.TYPE_APP) {
            view = View.inflate(mContext, R.layout.view_contract_suite_permission_app, null)
            mPermissionActionDeny = view.findViewById(R.id.textPermissionAppActionDeny)
            mPermissionActionAgree = view.findViewById(R.id.textPermissionAppActionAgree)
            mPermissionScrollView = view.findViewById(R.id.scrollPermissionAppContent)
            llPermissionAgreeContent = view.findViewById(R.id.llPermissionAppAgreeContent)
            llPermissionActionContent = view.findViewById(R.id.llPermissionAppActionContent)
            llPermissionDenyContent = view.findViewById(R.id.llPermissionAppDenyContent)
            textPermissionActionCheckbox = view.findViewById(R.id.textPermissionAppActionCheckbox)
            imagePermissionActionCheckbox = view.findViewById(R.id.imagePermissionAppActionCheckbox)
        }

        //判断是否已经授权
        if (item.result == PermissionItem.RESULT_AGREE) {
            llPermissionAgreeContent.visibility = View.VISIBLE
            llPermissionActionContent.visibility = View.GONE
            llPermissionDenyContent.visibility = View.GONE

        } else if (item.result == PermissionItem.RESULT_DENY) {
            llPermissionAgreeContent.visibility = View.GONE
            llPermissionActionContent.visibility = View.GONE
            llPermissionDenyContent.visibility = View.VISIBLE

        } else {
            llPermissionDenyContent.visibility = View.GONE
            llPermissionAgreeContent.visibility = View.GONE
            llPermissionActionContent.visibility = View.VISIBLE
        }

        println("holder------ type: ${item.type}  position: $position result: ${item.result}")

        //更新点击展示
        if (item.hadReadContent) {
            mPermissionActionAgree.setBackgroundResource(R.drawable.contract_suite_permission_agree)
        }

        //滑动到底监听
        mPermissionScrollView.setOnScrollChangeListener { v: NestedScrollView, _: Int, scrollY: Int, _: Int, _: Int ->
            try {
                val edg = v.getChildAt(0).measuredHeight - v.measuredHeight
                if (scrollY >= edg) {
                    item.hadReadContent = true
                    mPermissionActionAgree.setBackgroundResource(R.drawable.contract_suite_permission_agree)

                    item.hadReadContent = true
                    imagePermissionActionCheckbox.tag = "YES"
                    imagePermissionActionCheckbox.setImageResource(R.drawable.contract_suite_checkbox_permission_yes)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                item.hadReadContent = true
                mPermissionActionAgree.setBackgroundResource(R.drawable.contract_suite_permission_agree)
            }
        }

        //点击事件
        textPermissionActionCheckbox.setOnClickListener {
            val gtag = imagePermissionActionCheckbox.tag as String
            println("------1 $gtag")
            if (gtag.equals("NO", true)) {
                item.hadReadContent = true
                imagePermissionActionCheckbox.tag = "YES"
                imagePermissionActionCheckbox.setImageResource(R.drawable.contract_suite_checkbox_permission_yes)
            } else {
                item.hadReadContent = false
                imagePermissionActionCheckbox.tag = "NO"
                imagePermissionActionCheckbox.setImageResource(R.drawable.contract_suite_checkbox_permission_no)
            }
        }
        imagePermissionActionCheckbox.setOnClickListener {
            val gtag = imagePermissionActionCheckbox.tag as String
            println("------2 $gtag")
            if (gtag.equals("NO", true)) {
                item.hadReadContent = true
                imagePermissionActionCheckbox.tag = "YES"
                imagePermissionActionCheckbox.setImageResource(R.drawable.contract_suite_checkbox_permission_yes)
            } else {
                item.hadReadContent = false
                imagePermissionActionCheckbox.tag = "NO"
                imagePermissionActionCheckbox.setImageResource(R.drawable.contract_suite_checkbox_permission_no)
            }
        }

        mPermissionActionDeny.setOnClickListener {
            mItemClickListener.invoke(PermissionItem.RESULT_DENY, item, position)
            //打点
            mPermissionActionDeny.recordContractSuiteClickContractSuiteEvent()
        }
        mPermissionActionAgree.setOnClickListener {
            if (item.hadReadContent) {
                mItemClickListener.invoke(PermissionItem.RESULT_AGREE, item, position)
            } else {
                //展示
                mfragmentManager?.let { manager ->
                    mPermissionAskDialog.setOnActionListener { action ->
                        if (1 == action) {
                            item.hadReadContent = true
                            imagePermissionActionCheckbox.tag = "YES"
                            imagePermissionActionCheckbox.setImageResource(R.drawable.contract_suite_checkbox_permission_yes)
                            mItemClickListener.invoke(PermissionItem.RESULT_AGREE, item, position)
                        }
                    }
                    mPermissionAskDialog.showNow(
                        manager,
                        PermissionsDialog::class.java.simpleName
                    )
                }
//                Toast.makeText(
//                    mContext,
//                    mContext.getString(R.string.contract_suite_scroll_to_bottom_tips),
//                    Toast.LENGTH_SHORT
//                ).show()
            }
            //打点
            mPermissionActionAgree.recordContractSuiteClickContractSuiteEvent()
        }


        //添加到父控件
        container.addView(view)
        return view
    }

    override fun getItemPosition(obj: Any): Int {
//        if (dataList.isEmpty()) {
//            return POSITION_NONE;
//        }
        return POSITION_NONE;
    }

    override fun getCount(): Int {
        return dataList.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
        //从viewpager中移除掉
        container.removeView(obj as View)
    }

    /**
     * 设置点击回调
     *
     * @param Int 位置
     * @param RevObjAiHomeDemo 值
     * @param Int 类型
     */
    fun setItemClickListener(listener: (Int, PermissionItem, Int) -> Unit) {
        mItemClickListener = listener
    }

    /** 设置 fragemnt manager */
    fun setFragmentManager(fragmentManager: FragmentManager?) {
        mfragmentManager = fragmentManager
    }
}